import java.util.Arrays;
/**
 * This class is a subclass of the Hand class, and are used to model a hand of Flush. 
 * It overrides getTopCard,isValid and getType method that it inherits from Hand class.
 * 
 * @author Pranav Talwar
 *
 */
@SuppressWarnings("serial")
public class Flush extends Hand{
		
	/**
	 * Constructor for Flush type hand. Calls the constructor of Hand superclass.
	 * 
	 * @param player Player who plays the hand
	 * @param cards  List of card played by the player
	 */
	public Flush(CardGamePlayer player, CardList cards) {
		super(player, cards);
	}
	
	/* (non-Javadoc)
	 * Returns the top card of the hand.
	 * 
	 * @see Hand#getTopCard()
	 */
	public Card getTopCard() {
		int[] handranks = new int[5];
		for(int i=0;i<5;i++) {
			if(this.getCard(i).getRank()==0) {
				handranks[i] = 13;
			}
			else if(this.getCard(i).getRank()==1) {
				handranks[i] = 14;
			}
			else {
				handranks[i] = this.getCard(i).getRank();
			}
		}
		Arrays.sort(handranks);
		if(handranks[4]>=13) {
			handranks[4]-=13;
		}
		int returnIndex = 0;
		for(int i=1;i<5;i++) {
			if(this.getCard(i).getRank() == handranks[4]) {
				returnIndex = i;
			}
		}
		return this.getCard(returnIndex);
	}
	
	/* (non-Javadoc)
	 * Checks whether the hand is a Flush.
	 * 
	 * @see Hand#isValid()
	 */
	public boolean isValid() {
		if(this.size() == 5) {
			boolean flag = true;
			int suit = this.getCard(0).getSuit();
			for(int i=1;i<this.size();i++) {
				if(suit != this.getCard(i).getSuit()) {
					flag = false;
					break;
				}
			}
			return flag;
		}
		return false;
	}
	
	/* (non-Javadoc)
	 * Returns type of string.
	 * 
	 * @see Hand#getType()
	 */
	public String getType() {
		return "Flush";	
	}
	
	

}
